package com.toogu.mazhangmen.util;

import android.app.Activity;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.WebSocketListener;


public class OkhttpUtil {
    //获取OkHttpClient对象时返回这个，单例设计
    private volatile static OkhttpUtil okhttpUtil;
    private volatile static OkHttpClient okHttpClient;

    private static TokenUtil tokenUtil = new TokenUtil();
//测试环境
//    public static String urlHead = "https://door.toogu.cc";
//    public static String wss="wss://door.toogu.cc:9002/wss";
//正式环境
    public static String urlHead = "https://mzm.toogu.cc";
    public static String wss="wss://mzm.toogu.cc:9002/wss";

    private OkhttpUtil() {
        okHttpClient = new OkHttpClient.Builder().build();
    }

    public  OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public  static OkhttpUtil getOkhttpUtil() {
        if (okhttpUtil == null) {
            synchronized (OkhttpUtil.class) {
                if (okhttpUtil == null) {
                    okhttpUtil = new OkhttpUtil();
                }
            }
        }
        return okhttpUtil;
    }
    public void cancelTag(Object tag) {
        for (Call call : okHttpClient.dispatcher().queuedCalls()) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }
        for (Call call : okHttpClient.dispatcher().runningCalls()) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }
    }

    //通过post的方式发送异步请求
    public  void sentPost(Activity activity,String url, RequestBody requestBody, Callback callback) {
        url = urlHead+url;
        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("Authorization",tokenUtil.getToken_type()+" "+tokenUtil.getAccess_token())
                .tag(activity)
                .url(url).post(requestBody).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    //通过get的方式发送异步请求
    public void sentGet(Activity activity,String url, Callback callback) {
        url = OkhttpUtil.urlHead + url;
        Request request = new Request.Builder()
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("Authorization",tokenUtil.getToken_type()+" "+tokenUtil.getAccess_token())
                .tag(activity)
                .url(url).get().build();
        okHttpClient.newCall(request).enqueue(callback);
    }
    //通过put发送同步请求
    public void sentPut(Activity activity,String url, RequestBody requestBody, Callback callback){
        url = OkhttpUtil.urlHead + url;
        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .addHeader("Authorization",tokenUtil.getToken_type()+" "+tokenUtil.getAccess_token())
                .tag(activity)
                .url(url).put(requestBody).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    public void webSock(WebSocketListener webSocketListener) {
        Request request = new Request.Builder()
                .addHeader("Content-Type", "application/json")
                .addHeader("Origin", urlHead)
//                .addHeader("X-Requested-With", "XMLHttpRequest")
//                .addHeader("Authorization", new TokenUtil().getToken_type() + " " + new TokenUtil().getAccess_token())
                .url(wss)
                .build();
        okHttpClient.newWebSocket(request, webSocketListener);
    }
}
